﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T.FAS.Runtime.DataAccess.SQLDom
{
    /// <summary>
    /// 查询过滤器
    /// </summary>
    [Serializable]
    public class QueryFilter
    {
        private QueryGroup queryGroup;

        /// <summary>
        /// 括号分组
        /// </summary>
        public QueryGroup QueryGroup {
            get
            {
                return queryGroup;
            }
            set
            {
                //给QueryGroup赋值时，自动扁平化赋值给FilterClasuse
                queryGroup = value;
                FlatQueryGroup();
            }
        }


        /// <summary>
        /// 过滤条件
        /// </summary>
        public List<FilterClause> FilterClauses { get; set; } = new List<FilterClause>();

        /// <summary>
        /// 排序条件
        /// </summary>
        public List<OrderByClause> OrderByCondition { get; set; } = new List<OrderByClause>();

        /// <summary>
        /// 聚合条件
        /// </summary>
        public List<AggregateClause> AggregateClause { get; set; } = new List<AggregateClause>();

        /// <summary>
        /// 分组条件
        /// </summary>
        public List<GroupByClause> GroupByClause { get; set; } = new List<GroupByClause>();

        /// <summary>
        /// 分页信息
        /// </summary>
        public PageParameter PageParameter { get; set; }

        /// <summary>
        /// 限制数据返回行数
        /// </summary>
        public int LimitCount { get; set; }

        //查询投影字段
        private List<QueryProjectionField> projectionFields;

        /// <summary>
        /// 查询投影字段
        /// </summary>
        public List<QueryProjectionField> ProjectionFields
        {
            get
            {
                if (projectionFields == null)
                    projectionFields = new List<QueryProjectionField>();

                return projectionFields;
            }
            set
            {
                projectionFields = value;
            }
        }

        ///// <summary>
        ///// 分区分表键值
        ///// </summary>
        //public ShardingValue ShardingValue { get; set; }

        private void FlatQueryGroup(QueryGroup queryGroup = null)
        {
            if(queryGroup == null)
            {
                if (QueryGroup == null)
                    return;
                else
                    queryGroup = QueryGroup;
            }
            if (queryGroup.FilterClauses != null && queryGroup.FilterClauses.Count > 0 && FilterClauses != null)
                FilterClauses.AddRange(queryGroup.FilterClauses);
            if (queryGroup.QueryGroups != null && queryGroup.QueryGroups.Count > 0)
            {
                foreach (var group in queryGroup.QueryGroups)
                {
                    FlatQueryGroup(group);
                }
            }
        }
    }
}
